package pack;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.BiletDAORemote;
import dao.EmployeeDAORemote;
import dto.BiletDTO;
import dto.EmployeeDTO;

@ManagedBean(name = "angajatBean")
@SessionScoped
public class AngajatBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@EJB
	BiletDAORemote biletDAO;

	@ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employeeDTO; // = new EmployeeDTO();

	@ManagedProperty(value = "#{biletDTO}")
	private BiletDTO biletDTO;

	

	private int id;

	private String lastname;

	private String firstname;

	private String password;

	private String email;

	private String rankS;

	private String username;

	private String department;

	private String areaManager;

	private String position;

	private String contactpersonde;

	private String teamleaderro;

	private String empl_type;

	private String var_part;

	private Double net_salary;

	private int prosisnr;

	private String profile;

	private String dn_an;

	private String dn_lu;

	private String dn_zi;

	private String vm_an;

	private String vm_lu;

	private String vm_zi;

	private String da_an;

	private String da_lu;

	private String da_zi;

	private String dp_an;

	private String dp_lu;

	private String dp_zi;
	
	List<EmployeeDTO> angajati;
	
	EmployeeDTO angajatiUpd;

	public String inapoi() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if (eDTO != null) {

			setEmail(eDTO.getEmail());
			setDateofbirth(eDTO.getDateofbirth());
			setFirstname(eDTO.getFirstname());
			setLastname(eDTO.getLastname());
			setNet_salary(eDTO.getNet_salary());
			setTeamleaderro(eDTO.getTeamleaderro());
			setAreaManager(eDTO.getAreaManager());
			setContactpersonde(eDTO.getContactpersonde());
			setRank(eDTO.getRank());
			setUsername(eDTO.getUsername());
		} else
			System.out.println("Error: ");

		return "administrator.xhtml";
	}
	
	public void verifyA(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 1){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}
	
	public void verifyS(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 4){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}
	
	public void verifyHR(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 2){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}

	public void resetFields() {
		setLastname("");
		setFirstname("");
		setPassword("");
		setEmail("");
		setRank(5);
		setUsername("");
		setDepartment("");
		setAreaManager("");
		setAreaManager("");
		setPosition("");
		setContactpersonde("");
		setTeamleaderro("");
		setEmpl_type("");
		setVar_part("");
		setNet_salary(0.0);
		setProsisnr(0);
		setProfile("");
		setDa_an("2005");
		setDa_lu("01");
		setDa_zi("01");
		setDp_an("2011");
		setDp_lu("01");
		setDp_zi("01");
		setDn_an("1975");
		setDn_lu("01");
		setDn_zi("01");
		setVm_an("2001");
		setVm_lu("01");
		setVm_zi("01");
	}
	
	public String read(){
		angajati = employeeDAO.findAllAngajati();
		this.setAngajati(angajati);
		return "";
	}

	public String create() {
		EmployeeDTO eDTO = new EmployeeDTO();
		eDTO.setLastname(getLastname());
		eDTO.setFirstname(getFirstname());
		eDTO.setLastmedicalvisit(getLastmedicalvisit());
		eDTO.setPassword(getPassword());
		eDTO.setEmail(getEmail());
		eDTO.setRank(getRank());
		eDTO.setUsername(getUsername());
		eDTO.setDepartment(getDepartment());
		eDTO.setDateofbirth(getDateofbirth());
		eDTO.setAreaManager(getAreaManager());
		eDTO.setAreaManager(getAreaManager());
		eDTO.setPosition(getPosition());
		eDTO.setContactpersonde(getContactpersonde());
		eDTO.setTeamleaderro(getTeamleaderro());
		eDTO.setEmpl_type(getEmpl_type());
		eDTO.setVar_part(getVar_part());
		eDTO.setNet_salary(getNet_salary());
		eDTO.setDeparture_date(getDeparture_date());
		eDTO.setEmployment_date(getEmployment_date());
		eDTO.setProsisnr(getProsisnr());
		eDTO.setProfile(getProfile());
		
		employeeDAO.add(eDTO);
		resetFields();
		return "";
	}
	
	public String edit(EmployeeDTO employee){
		setAngajatiUpd(employee);
		return "angajatiUpdate";
	}
	
	public String update(){
		employeeDAO.edit(angajatiUpd);
		return "";
	}
	
	public String delete(EmployeeDTO employee){
		employeeDAO.del(employee);
		return "";
	}

	public String setDateDN(){
		String dd = "";
		dd =dd+getDn_an()+"-"+getDn_lu()+"-"+getDn_zi();
		return dd;
	}
	
	public String setDateDA(){
		String dd = "";
		dd =dd+getDa_an()+"-"+getDa_lu()+"-"+getDa_zi(); 
		return dd;
	}
	
	public String setDateVM(){
		String dd = "";
		dd =dd+getVm_an()+"-"+getVm_lu()+"-"+getVm_zi(); 
		return dd;
	}
	
	public String setDateDP(){
		String dd = "";
		dd =dd+getDp_an()+"-"+getDp_lu()+"-"+getDp_zi(); 
		return dd;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastmedicalvisit() {
		return this.setDateVM();
	}

	public void setLastmedicalvisit(String lastmedicalvisit) {
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getRank() {
		if (rankS.equals("Administrator"))
			return 1;
		if (rankS.equals("Resurse umane"))
			return 2;
		if (rankS.equals("Team leader"))
			return 3;
		if (rankS.equals("Secretara"))
			return 4;
		
		return 5;
	}

	public void setRank(int rank) {
			setRankS("Angajat");
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getDateofbirth() {
		return this.setDateDN();
	}

	public void setDateofbirth(String dateofbirth) {
	}

	public String getAreaManager() {
		return areaManager;
	}

	public void setAreaManager(String areaManager) {
		this.areaManager = areaManager;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getContactpersonde() {
		return contactpersonde;
	}

	public void setContactpersonde(String contactpersonde) {
		this.contactpersonde = contactpersonde;
	}

	public String getTeamleaderro() {
		return teamleaderro;
	}

	public void setTeamleaderro(String teamleaderro) {
		this.teamleaderro = teamleaderro;
	}

	public String getEmpl_type() {
		return empl_type;
	}

	public void setEmpl_type(String empl_type) {
		this.empl_type = empl_type;
	}

	public String getVar_part() {
		return var_part;
	}

	public void setVar_part(String var_part) {
		this.var_part = var_part;
	}

	public Double getNet_salary() {
		return net_salary;
	}

	public void setNet_salary(Double net_salary) {
		this.net_salary = net_salary;
	}

	public String getDeparture_date() {
		return this.setDateDP();
	}

	public void setDeparture_date(String departure_date) {
	}

	public String getEmployment_date() {
		return this.setDateDA();
	}

	public void setEmployment_date(String employment_date) {
	}

	public int getProsisnr() {
		return prosisnr;
	}

	public void setProsisnr(int prosisnr) {
		this.prosisnr = prosisnr;
	}

	public String getProfile() {
		return profile;
	}

	public void setProfile(String profile) {
		this.profile = profile;
	}

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public BiletDTO getBiletDTO() {
		return biletDTO;
	}

	public void setBiletDTO(BiletDTO biletDTO) {
		this.biletDTO = biletDTO;
	}

	public String getDn_an() {
		return dn_an;
	}

	public void setDn_an(String dn_an) {
		this.dn_an = dn_an;
	}

	public String getDn_lu() {
		return dn_lu;
	}

	public void setDn_lu(String dn_lu) {
		this.dn_lu = dn_lu;
	}

	public String getDn_zi() {
		return dn_zi;
	}

	public void setDn_zi(String dn_zi) {
		this.dn_zi = dn_zi;
	}

	public String getDa_an() {
		return da_an;
	}

	public void setDa_an(String da_an) {
		this.da_an = da_an;
	}

	public String getDa_lu() {
		return da_lu;
	}

	public void setDa_lu(String da_lu) {
		this.da_lu = da_lu;
	}

	public String getDa_zi() {
		return da_zi;
	}

	public void setDa_zi(String da_zi) {
		this.da_zi = da_zi;
	}

	public String getDp_an() {
		return dp_an;
	}

	public void setDp_an(String dp_an) {
		this.dp_an = dp_an;
	}

	public String getDp_lu() {
		return dp_lu;
	}

	public void setDp_lu(String dp_lu) {
		this.dp_lu = dp_lu;
	}

	public String getDp_zi() {
		return dp_zi;
	}

	public void setDp_zi(String dp_zi) {
		this.dp_zi = dp_zi;
	}

	public String getVm_an() {
		return vm_an;
	}

	public void setVm_an(String vm_an) {
		this.vm_an = vm_an;
	}

	public String getVm_lu() {
		return vm_lu;
	}

	public void setVm_lu(String vm_lu) {
		this.vm_lu = vm_lu;
	}

	public String getVm_zi() {
		return vm_zi;
	}

	public void setVm_zi(String vm_zi) {
		this.vm_zi = vm_zi;
	}

	public String getRankS() {
		return rankS;
	}

	public void setRankS(String rankS) {
		this.rankS = rankS;
	}

	public List<EmployeeDTO> getAngajati() {
		return angajati;
	}

	public void setAngajati(List<EmployeeDTO> angajati) {
		this.angajati = angajati;
	}

	public EmployeeDTO getAngajatiUpd() {
		return angajatiUpd;
	}

	public void setAngajatiUpd(EmployeeDTO angajatiUpd) {
		this.angajatiUpd = angajatiUpd;
	}
}
